<template>
<!-- x分级渲染未完成  包括新增中的-->
  <div class="user_box">
    <!-- 顶部搜索 -->
    <el-form :inline="true" class="demo-form-inline">
      <!-- 新增user -->
      <el-button
        class="addBtn"
        type="primary"
        icon="el-icon-edit"
        @click="dialogBtn('create')"
        >添加菜单</el-button
      >
      <!-- 新增的表单 -->
      <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible">
        <el-form :model="form" :rules="rules" label-width="100px">
          <el-form-item label="权限组名称" :label-width="formLabelWidth">
            <el-radio-group
              v-model="type"
              class="choose-type"
            >
              <el-radio label="menu" class="choose-item" :disabled="typeStatus"
                >菜单</el-radio
              >
              <el-radio
                label="points"
                class="choose-item"
                :disabled="typeStatus"
                >权限点</el-radio
              >
            </el-radio-group>
          </el-form-item>
          <!-- <el-form-item label="权限组名称" :label-width="formLabelWidth">
          </el-form-item> -->
          <el-form-item label="权限组名称">
            <el-select v-model="form.pid">
              <el-option :value="0" :label="$t('table.powerNav')"
                >主导航</el-option
              >
              <el-option
                v-for="items in data"
                :value="items.id"
                :key="items.id"
                :label="items.title"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item
            label="权限代码"
            :label-width="formLabelWidth"
            prop="code"
          >
            <el-input v-model="form.code"></el-input>
          </el-form-item>

          <el-form-item label="权限标题" prop="title">
            <el-input type="textarea" v-model="form.title"></el-input>
          </el-form-item>
        </el-form>

        <div slot="footer" class="dialog-footer">
          <el-button @click="cancelBtn">取 消</el-button>
          <el-button type="primary" @click="addForm">确 定</el-button>
        </div>
      </el-dialog>
    </el-form>

    <!-- table表单 -->
    <template>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="title" label="标题" width="180">
          <!-- <el-tree :data="data" :props="defaultProps"></el-tree> -->
        </el-table-column>
        <el-table-column prop="code" label="权限代码" width="721">
        </el-table-column>

        <!--右侧标签 默认插槽 -->
        <el-table-column label="操作" width="200">
          <template #default="{ row }">
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              class="edit1"
              @click="editBtn('edit', row.id)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              class="edit2"
              @click="del(row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>
  </div>
</template>

<script>
import {
  list,
  add,
  remove,
  update,
  detail
} from '@/api/base/menus.js'
export default {
  data () {
    return {
      typeStatus: false,
      type: 'menu',
      formMenu: {
        pid: '', // 父级Id
        is_point: '', // 是否权限点
        code: '', // 菜单代码
        title: '' // 标题
      },
      formPoints: {
        pid: '', // 父级Id
        is_point: '', // 是否权限点
        code: '', // 菜单代码
        title: '' // 标题
      },
      tableData: [],
      data: [
        {
          label: '一级 1',
          children: [
            {
              label: '二级 1-1',
              children: [
                {
                  label: '三级 1-1-1'
                }
              ]
            }
          ]
        }
      ],

      id: null,
      dialogType: 'create',
      rules: {
        title: [
          { required: true, message: '请输入代码', trigger: 'blur' }
        ],
        code: [{ required: true, message: '请输入标题', trigger: 'blur' }]
      },

      dialogTableVisible: false,
      dialogFormVisible: false,
      form: {
        code: '',
        title: '',
        pid: 1,
        isPoint: true
      },
      formLabelWidth: '100px'

    }
  },

  async created () {
    const { data } = await list()
    // console.log(data)
    this.tableData = data
  },

  methods: {
    // 提示对话框
    open () {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },

    // dialog 新增用户的功能
    dialogBtn (type) {
      this.dialogType = type
      // if (this.dialogType === 'create') {
      this.dialogFormVisible = true
    },
    cancelBtn () {
      this.dialogType = 'create'
      this.clearAddform()
      this.dialogFormVisible = false
    },

    clearAddform () {
      this.form.code = ''
      this.form.title = ''
      this.form.pid = ''
      this.form.isPoint = ''
    },
    // 表单点击确认
    async addForm () {
      try {
        if (this.dialogType === 'create') {
          // this.formInline.province = this.form.province
          // this.formInline.city = this.form.city
          console.log(this.form)
          await add(this.form)
        }
        if (this.dialogType === 'edit') {
          await update({ id: this.id, ...this.form })
          this.renderList()
          // console.log(res)
        }
        this.clearAddform()
        this.dialogFormVisible = false
        this.$message.success(
          this.dialogType === 'create' ? '添加成功' : '编辑成功'
        )
        this.renderList()
      } catch (error) {
        this.$message.error('添加失败')
      }
    },

    // 渲染页面
    async renderList () {
      const { data } = await list(this.data)
      // console.log(data.items)
      this.tableData = data.items
      this.total = data.counts
      // this.form.state = data.items.state
    },

    // 修改按钮
    async editBtn (type, id) {
      this.id = id
      this.dialogType = type
      const { data } = await detail(id)
      console.log(data)
      this.form.code = data.code
      this.form.title = data.title
      this.form.pid = data.pid
      this.form.isPoint = data.isPoint
      this.dialogFormVisible = true
    },

    // 删除
    async del (id) {
      try {
        this.open()
        await remove(id)
        this.$message.success('删除成功')
        // this.renderList()
        // console.log(data)
      } catch (error) {
        this.$message.error('删除失败')
      }
    }

  },
  computed: {
    dialogTitle () {
      if (this.dialogType === 'create') return '添加用户'
      if (this.dialogType === 'statue') return '修改状态'
      if (this.dialogType === 'edit') return '修改用户'
      else {
        return '添加用户'
      }
    }
  }
}
</script>

<style scoped lang="less">
.user_box {
  width: 1091px;
  // height: 417.5px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  border: 1px solid #ebeef5;
  background-color: #fff;
  color: #303133;
  transition: 0.3s;
  border-radius: 4px;
  overflow: hidden;
  margin: 20px;
  padding: 20px;
}
.addBtn {
  float: right;
  margin-left: 10px;
  color: #fff;
  background-color: #67c23a;
  border-color: #67c23a;
  border: 1px solid #dcdfe6;
  font-weight: 500;
}
.m_alert {
  display: flex;
  background-color: #f4f4f5;
  color: #909399;
  width: 100%;
  padding: 8px 16px;
  margin: 0;
  border-radius: 4px;
  color: #909399;
  i {
    display: inline-block;
    font-size: 16px;
    width: 16px;
    line-height: 1;
    margin-right: 5px;
  }
  .m-text {
    font-size: 13px;
    line-height: 18px;
  }
  .actions {
    width: 60px;
    height: 36px;
    margin-right: 5px;
    // padding-left: 5px;
    // padding-right: 5px;
    .el-icon {
      width: 14px;
      height: 14px;
      font-size: 14px;
      padding: 10px;

      line-height: 1;
      border-radius: 50%;
      display: inline-block;
      line-height: 1;
      white-space: nowrap;
      cursor: pointer;
      border: 1px solid #dcdfe6;
      transition: 0.1s;
      font-weight: 500;
    }
    .el-icon-edit {
      margin: 0 5px;
      margin-top: 6px;
      color: #409eff;
      background: #ecf5ff;
      border-color: #b3d8ff;
    }
    .el-icon-delete {
      margin-right: 5px;
      color: #f56c6c;
      background: #fef0f0;
      border-color: #fbc4c4;
    }
  }
}
.edit1 {
  color: #409eff;
  background: #ecf5ff;
  border-color: #b3d8ff;
  &:hover {
    background-color: #409eff;
    color: #fff;
  }
}
.edit2 {
  color: #f56c6c;
  background: #fef0f0;
  border-color: #fbc4c4;
  &:hover {
    background-color: #f56c6c;
    color: #fff;
  }
}
</style>
